//
//  WPNewDiseaseInstructionView.m
//  wobingwoyi
//
//  Created by WGP on 2016/12/1.
//  Copyright © 2016年 WGP. All rights reserved.
//

#import "WPNewDiseaseInstructionView.h"
#import "UITableView+FDTemplateLayoutCell.h"
#import "WPDiseaseInstructionCell.h"
#import "WPDiseaseInstructionHeaderView.h"
#import "WPDiseaseInstructionModel.h"
#import "ZNBPresentAnimator.h"
#import "ZNBDismissAnimator.h"

@interface WPNewDiseaseInstructionView ()<UITableViewDelegate,UITableViewDataSource,WPDiseaseInstructionHeaderViewDelegate,UIViewControllerTransitioningDelegate>

@property (strong, nonatomic) UIPercentDrivenInteractiveTransition *interactionController;

@property(nonatomic,strong)UILabel *titleLabel;
@property(nonatomic,strong)UIView *navBarView;
@property(nonatomic,strong)UIButton *backBtn;
@property(nonatomic,strong)UITableView *tableView;

@property(nonatomic,strong)NSMutableArray *dataArr;

@end

@implementation WPNewDiseaseInstructionView

- (id<UIViewControllerAnimatedTransitioning>)animationControllerForPresentedController:(UIViewController *)presented presentingController:(UIViewController *)presenting sourceController:(UIViewController *)source {
    return [ZNBPresentAnimator new];
}

- (id<UIViewControllerAnimatedTransitioning>)animationControllerForDismissedController:(UIViewController *)dismissed {
    return [ZNBDismissAnimator new];
}

- (id<UIViewControllerInteractiveTransitioning>)interactionControllerForPresentation:(id<UIViewControllerAnimatedTransitioning>)animator {
    return nil;
}

- (id<UIViewControllerInteractiveTransitioning>)interactionControllerForDismissal:(id<UIViewControllerAnimatedTransitioning>)animator {
    return self.interactionController;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.transitioningDelegate = self;
    
    self.view.backgroundColor = GlobalBgColor;
    [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleDefault;
    [self setupUI];
    
    [self.tableView registerClass:[WPDiseaseInstructionCell class] forCellReuseIdentifier:@"WPDiseaseInstructionCell"];
    [self.tableView registerClass:[WPDiseaseInstructionHeaderView class] forHeaderFooterViewReuseIdentifier:@"WPDiseaseInstructionHeaderView"];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.allowsSelection = NO;
    
    [self loadData];
    
    self.titleLabel.text = self.disease;
}

-(void)loadData
{
    NSString *url = @"diseaseCategory/getCategoryDetail.do";
    NSDictionary *dict = @{@"name":self.disease};
    
    [[WPNetWorkManager sharedNetWorkManager] POST:url parameters:dict progress:^(NSProgress * _Nonnull uploadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        id dict = responseObject[@"detail"];
        if([responseObject[@"result"] isEqualToString:@"success"] && [dict isKindOfClass:[NSDictionary class]])
        {
            WPDiseaseInstructionModel *model1 = [WPDiseaseInstructionModel modelWithNSString:dict[@"introduction"]];
            WPDiseaseInstructionModel *model2 = [WPDiseaseInstructionModel modelWithNSString:dict[@"symptom"]];
            WPDiseaseInstructionModel *model3 = [WPDiseaseInstructionModel modelWithNSString:dict[@"identify"]];
            WPDiseaseInstructionModel *model4 = [WPDiseaseInstructionModel modelWithNSString:dict[@"help"]];
            
            [self.dataArr addObject:model1];
            [self.dataArr addObject:model2];
            [self.dataArr addObject:model3];
            [self.dataArr addObject:model4];
        }
        else
        {
            
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        [self showErrorInfoWithStatus:@"加载失败"];
    }];
}

-(void)setupUI
{
    [self navBarView];
    [self titleLabel];
    [self backBtn];
    [self tableView];
}

-(UIView *)navBarView
{
    if (_navBarView == nil)
    {
        UIView *stateView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 20)];
        stateView.backgroundColor = [UIColor whiteColor];
        [self.view addSubview:stateView];
        
        _navBarView = [[UIView alloc] initWithFrame:CGRectMake(0, 20, SCREEN_WIDTH, 44)];
        _navBarView.backgroundColor = [UIColor whiteColor];
        [self.view addSubview:_navBarView];
        
        UIImageView *lineView = [UIImageView lineImageView];
        [_navBarView addSubview:lineView];
        
        [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.bottom.equalTo(_navBarView);
            make.height.mas_equalTo(0.5);
        }];
    }
    return  _navBarView;
}

-(UILabel *)titleLabel
{
    if (_titleLabel == nil)
    {
        _titleLabel = [[UILabel alloc] init];
        _titleLabel.textColor = RGB_SAME(51);
        _titleLabel.font = [UIFont systemFontOfSize:18];
        [self.navBarView addSubview:_titleLabel];
        
        [_titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.center.equalTo(self.navBarView);
        }];
    }
    return _titleLabel;
}

-(UIButton *)backBtn
{
    if (_backBtn == nil)
    {
        _backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_backBtn setBackgroundImage:[UIImage imageNamed:@"back_normal"] forState:UIControlStateNormal];
        [_backBtn setBackgroundImage:[UIImage imageNamed:@"back_highlight"] forState:UIControlStateHighlighted];
        [_backBtn sizeToFit];
        [_backBtn addTarget:self action:@selector(backBtnDidClick) forControlEvents:UIControlEventTouchUpInside];
        [self.navBarView addSubview:_backBtn];
        
        [_backBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(self.navBarView);
            make.left.equalTo(self.navBarView).offset(10);
        }];
        
    }
    return _backBtn;
}


-(UITableView *)tableView
{
    if (_tableView == nil)
    {
        _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.backgroundColor = GlobalBgColor;
        [self.view addSubview:_tableView];
        
        [_tableView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.bottom.equalTo(self.view);
            make.top.equalTo(self.navBarView.mas_bottom);
        }];
    }
    return _tableView;
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 4;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (self.dataArr.count)
    {
        WPDiseaseInstructionModel *model = self.dataArr[section];
        return model.isShow ? 1 : 0;
    }
    else
    {
        return 0;
    }
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    WPDiseaseInstructionCell *cell = [tableView dequeueReusableCellWithIdentifier:@"WPDiseaseInstructionCell" forIndexPath:indexPath];
    WPDiseaseInstructionModel *model = self.dataArr[indexPath.item];
    cell.descpStr = model.str;
    return cell;
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    WPDiseaseInstructionHeaderView *headerView = [tableView dequeueReusableHeaderFooterViewWithIdentifier:@"WPDiseaseInstructionHeaderView"];
    headerView.section = section;
    headerView.delegate = self;
    
    if (section == 0)
    {
        headerView.label.text = @"疾病介绍";
        [headerView.iconView setBackgroundImage:[UIImage imageNamed:@"diseaseDescp_normal"] forState:UIControlStateNormal];
         [headerView.iconView setBackgroundImage:[UIImage imageNamed:@"diseaseDescp_selected"] forState:UIControlStateSelected];
    }
    else if(section == 1)
    {
        headerView.label.text = @"症状";
        [headerView.iconView setBackgroundImage:[UIImage imageNamed:@"diseaseSecond_normal"] forState:UIControlStateNormal];
        [headerView.iconView setBackgroundImage:[UIImage imageNamed:@"diseaseSecond_selected"] forState:UIControlStateSelected];
    }
    else if(section == 2)
    {
        headerView.label.text = @"检查诊断";
        [headerView.iconView setBackgroundImage:[UIImage imageNamed:@"diseaseThird_normal"] forState:UIControlStateNormal];
        [headerView.iconView setBackgroundImage:[UIImage imageNamed:@"diseaseThird_selected"] forState:UIControlStateSelected];
    }
    else
    {
        headerView.label.text = @"健康小贴士";
        [headerView.iconView setBackgroundImage:[UIImage imageNamed:@"diseaseFourth_normal"] forState:UIControlStateNormal];
        [headerView.iconView setBackgroundImage:[UIImage imageNamed:@"diseaseFourth_selected"] forState:UIControlStateSelected];
    }
    
    if(self.dataArr.count)
    {
        WPDiseaseInstructionModel *model = self.dataArr[section];
        headerView.contentView.backgroundColor = model.isShow ? [UIColor whiteColor] : GlobalBgColor;
        headerView.iconView.selected = model.isShow;
        headerView.bottomLine.hidden = model.isShow;
        
        if (model.isShow)
        {
            [UIView animateWithDuration:0.3 animations:^{
                
                headerView.arrowView.transform = CGAffineTransformRotate(headerView.arrowView.transform, M_PI);
            }];
        }
        else
        {
            [UIView animateWithDuration:0.3 animations:^{
                
                headerView.arrowView.transform = CGAffineTransformIdentity;
            }];
        }
    }
    else
    {
        headerView.backgroundColor = GlobalBgColor;
    }
    return headerView;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 50;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return [tableView fd_heightForCellWithIdentifier:@"WPDiseaseInstructionCell" cacheByIndexPath:indexPath configuration:^(WPDiseaseInstructionCell *cell) {
        // 配置 cell 的数据源，和 "cellForRow" 干的事一致，比如：
        WPDiseaseInstructionModel *model = self.dataArr[indexPath.item];
        cell.descpStr = model.str;
    }];
}

#pragma mark - WPDiseaseInstructionHeaderViewDelegate代理方法
-(void)WPDiseaseInstructionViewCoverBtnDidClick:(WPDiseaseInstructionHeaderView *)view
{
    if (self.dataArr.count)
    {
        WPDiseaseInstructionModel *model = self.dataArr[view.section];
        model.isShow = !model.isShow;
        
        [self.tableView reloadSections:[NSIndexSet indexSetWithIndex:view.section] withRowAnimation:UITableViewRowAnimationAutomatic];
    }
}

-(void)backBtnDidClick
{
    if (!self.isWhite)
    {
        [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleLightContent;
    }
    
    if (self.isPush)
    {
        [self.navigationController popViewControllerAnimated:YES];
    }
    else
    {
        [self dismissViewControllerAnimated:YES completion:nil];
    }
}

-(NSMutableArray *)dataArr
{
    if (_dataArr == nil)
    {
        _dataArr = [NSMutableArray array];
    }
    return _dataArr;
}

@end
